/**
 * [OElove] (C)2010-2099 OELOVE.COM Inc. X
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2016/11/24 by CL $
*/
$(function(){

	$(document).on("click", "[f='gettype']", function(){
		$_id = $(this).attr("data-id");
		$("#gettype").val($_id);
		$(".type dd label").removeClass("current");
		$(this).addClass("current");
		if ($_id == '1') {
			$(".email_type").show();
			$(".mobile_type").hide();
		}
		else if ($_id == '2') {
			$(".mobile_type").show();
			$(".email_type").hide();
		}
	});
	
	$(document).on("blur", "#mobile", function(){
		if ($(this).val().length > 0) {
			$("#mobile_tips").hide();
		}
	});
	//获取手机验证码
	$(document).on("click", "[f='send_mobilecode']", function(){
		$val_mobile = $("#mobile").val();
		if ($val_mobile.length != 11) {
			$("#mobile_tips").show();
			return false;
		}
		if (!isMobile($val_mobile)) {
			$("#mobile_tips").show();
			return false;
		}
		$(this).html("获取中...");
		$.ajax({
			type: "POST",
			url: _ROOT_PATH + "index.php?c=passport&a=sendforget",
			cache: false,
			data: {type:"2", name:$val_mobile, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					ToastShow("发送成功，请查收手机短信！");
					_countDown("btn_send_mobilecode", "send_mobilecode");
				}
				else {
					if ($result.length > 0) {
						ToastShow($result);
					}
					else {
						ToastShow("发送失败，请检查手机号码是否可用");
					}
					
				}
				$("#btn_send_mobilecode").html("获取验证码");

			},
			error: function() {
				ToastShow("系统繁忙，请稍后再试");
				$("#btn_send_mobilecode").html("获取验证码");
			}
		});

	});


	$(document).on("blur", "#email", function(){
		if ($(this).val().length > 0) {
			$("#email_tips").hide();
		}
	});
	//获取邮箱验证码
	$(document).on("click", "[f='send_emailcode']", function(){
		$val_email = $("#email").val();
		if ($val_email.length  == 0) {
			$("#email_tips").show();
			return false;
		}
		$(this).html("获取中...");
		$.ajax({
			type: "POST",
			url: _ROOT_PATH + "index.php?c=passport&a=sendforget",
			cache: false,
			data: {type:"1", name:$val_email, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					ToastShow("发送成功，请查收邮件");
					_countDown("btn_send_emailcode", "send_emailcode");
				}
				else {
					if ($result.length > 0) {
						ToastShow($result);
					}
					else {
						ToastShow("发送失败，请检查邮箱是否可用");
					}
					
				}
				$("#btn_send_emailcode").html("获取验证码");

			},
			error: function() {
				ToastShow("系统繁忙，请稍后再试");
				$("#btn_send_emailcode").html("获取验证码");
			}
		});

	});
	
	
	$(document).on("blur", "#mobilecode", function(){
		if ($(this).val().length > 0) {
			$("#mobilecode_tips").hide();
		}
	});
	$(document).on("blur", "#emailcode", function(){
		if ($(this).val().length > 0) {
			$("#emailcode_tips").hide();
		}
	});
	$(document).on("blur", "#newpassword", function(){
		if ($(this).val().length > 0) {
			$("#newpassword_tips").hide();
		}
	});
	$(document).on("blur", "#confirmpassword", function(){
		if ($(this).val().length > 0) {
			$("#confirmpassword_tips").hide();
		}
	});
	
	//提交保存
	$(document).on("click", "[f='but_forgetpost']", function(){

		$gettype = $("#gettype").val();
		$newpassword = $("#newpassword").val();
		$confirmpassword = $("#confirmpassword").val();
		
		if ($gettype == "1") { //邮箱取回
			$name = $("#email").val();
			$validcode = $("#emailcode").val();
			if ($name.length == 0) {
				$("#email_tips").show();
				return false;
			}
			if ($validcode.length == 0) {
				$("#emailcode_tips").show();
				return false;
			}
		}
		else {
			$name = $("#mobile").val();
			$validcode = $("#mobilecode").val();
			if ($name.length == 0) {
				$("#mobile_tips").show();
				return false;
			}
			if ($validcode.length == 0) {
				$("#mobilecode_tips").show();
				return false;
			}
		}


		if ($newpassword.length < 6 || $newpassword.length > 16) {
			$("#newpassword_tips").show();
			return false;
		}
		if ($confirmpassword != $newpassword) {
			$("#confirmpassword_tips").show();
			return false;
		}

		$.ajax({
			type: "POST",
			url: _ROOT_PATH + "index.php?c=passport&a=setpassword",
			cache: false,
			data: {type:$gettype, name:$name, validcode:$validcode, password:$newpassword, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					ToastShow("取回密码成功，请记住新密码！");
					setTimeout(function(){
						window.location.href = _ROOT_PATH+"index.php";
					}, 800);
				}
				else {
					if ($result.length > 0) {
						ToastShow($result);
					}
					else {
						ToastShow("取回密码失败，请检查输入是否正确。");
					}
				}

			},
			error: function() {
				ToastShow("系统繁忙，请稍后再试");
			}
		});


	});

});

//倒计时
var cd_timer = 60; //1分钟
function _countDown(obj_id, attr_f) {
	$obj = $("#"+obj_id);
	if (cd_timer == 0) {
		$obj.html("获取验证码");
		$obj.removeClass("current");
		$obj.attr("f", attr_f);
		cd_timer = 60; 
	}
	else {
		$obj.removeClass("current");
		$obj.addClass("current"); 
		$obj.html(cd_timer+"秒重新发送");
		$obj.attr("f", "");
		cd_timer--; 
		setTimeout(function() { 
			_countDown(obj_id, attr_f)
		},	1000);
	}
} 
